﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class Node : MonoBehaviour{
	public Vector3 Pos;
	public bool Walkable;
	public bool NPCWalcable = true;

	public float HeuristicValue;	//h -> distancia del nodo al nodo destino.
	public float MovementCost;	//g
	public float FValue;			//f combinacion de h y g;

	public Node Parent;			//este nodo es hijo de...
	public LineRenderer LRDebug;
	void Start(){
		Pos = transform.position;
	}
	public void CalculateNodeAtributes(Node targetNode){
		CalculateHeuristicValue (targetNode);
		CalculateMovementCost ();
		CalculateFValue ();
	}
	void FixedUpdate(){
		if (Parent) {
			LRDebug.SetPosition(0,transform.position+new Vector3(0,0.1f,0));
			LRDebug.SetPosition(1,Parent.Pos+new Vector3(0,0.1f,0));
		}
	}
	void CalculateHeuristicValue(Node targetNode){
		int distX = (int)Mathf.Abs (Pos.x - targetNode.Pos.x);	//distancia o cantidad de tiles en x
		int distY = (int)Mathf.Abs (Pos.z - targetNode.Pos.z);	//distancia o cantidad de tiles en y(z)
		HeuristicValue = distX + distY; //Distancia total al objetivo.
	}

	void CalculateMovementCost(){
		//it can be 10 or 14 http://es.ncalculators.com/geometry/length-between-two-points-calculadora.htm
		//0 if is the start point(enemy or player).
		if (Parent) {
			float dist = Vector3.Distance (Parent.Pos, Pos);

			// when the distance is 1.2, it is a diagonal
			MovementCost = (dist > 1.2) ? Mathf.Sqrt(2) : 1;
			MovementCost += Parent.MovementCost;
		}
		else {
			MovementCost = 0;
		}
	}

	void CalculateFValue(){
		FValue = HeuristicValue + MovementCost;
	}

	public void SetPathColor(Material a){
		LRDebug.material = a;
	}
}
